﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Library;
using System.Data;
using IHISCETeim.BLL;
using IHISCETeim.Model;
using System.Text;


namespace IHISCETeim.Web.MachineSys.PartsModel
{
    public partial class PartsDebtList : EasyUIPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看, Permission.修改, Permission.新增, Permission.删除,Permission.应收帐款_冲帐 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "PartsDebtList";
            }
        }

        #region

        [QueryColumnName(), QueryOperator(QueryOperatorsEnum.包含)]
        public string CardName
        {
            get
            {
                return GetRequestValue<string>("CardName", "");

            }
        }
        [QueryColumnName(), QueryOperator(QueryOperatorsEnum.包含)]
        public string OINVNum
        {
            get
            {
                return GetRequestValue<string>("OINVNum", "");

            }
        }
        [QueryColumnName(), QueryOperator(QueryOperatorsEnum.包含)]
        public string ORDRNum
        {
            get
            {
                return GetRequestValue<string>("ORDRNum", "");

            }
        }
        [QueryColumnName(), QueryOperator(QueryOperatorsEnum.包含)]
        public string ODLNNum
        {
            get
            {
                return GetRequestValue<string>("ODLNNum", "");

            }
        }
        [QueryColumnName(), QueryOperator(QueryOperatorsEnum.包含)]
        public string Comments
        {
            get
            {
                return GetRequestValue<string>("Comments", "");

            }
        }


        public string SaleDateFrom
        {
            get
            {
                return GetRequestValue<string>("SaleDateFrom", "");

            }
        }
        public string SaleDateTo
        {
            get
            {
                if (string.IsNullOrEmpty(SaleDateFrom))
                {
                    return "";
                }
                else
                {
                    string todate = GetRequestValue<string>("SaleDateTo", "");
                    if (string.IsNullOrEmpty(todate))
                        return Convert.ToDateTime(SaleDateFrom).AddMonths(1).ToShortDateString();
                    else
                        return todate;
                }

            }
        }
        #endregion

        /// <summary>
        /// 排序字段 值为 id
        /// </summary>
        protected override string orderField
        {
            get
            {
                return string.IsNullOrEmpty(base.orderField) ? "DebtId" : base.orderField;
            }
        }
        /// <summary>
        ///  排序方式 desc或 asc
        /// </summary>
        protected override string orderDirection
        {
            get
            {
                return string.IsNullOrEmpty(base.orderDirection) ? "desc" : base.orderDirection;
            }
        }
        protected override string SQLSort
        {
            get
            {                
                return "SaleDate Desc,"+ (orderField + " " + orderDirection + "  ");
            }
        }        

        protected string GetSQLWhere()
        {
            string sqlwhere = QueryFiledHelper.GetSqlWhere(this);
            if (!string.IsNullOrEmpty(SaleDateFrom))
            {
                string DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("SaleDate", QueryOperatorsEnum.范围, SaleDateFrom + "|" + SaleDateTo);
                if (!string.IsNullOrEmpty(sqlwhere))
                {
                    sqlwhere += " and " + DOWNSQL;
                }
                else
                {
                    sqlwhere = DOWNSQL;
                }
            }

            return sqlwhere;
        }
        protected override string query()
        {

            // return "";
            DataTableCollection dts = PartsDebtController.GetPagination(GetSQLWhere(), pageNum, numPerPage, SQLSort, ref TotalCount);

            return ToDataGridJson(dts[0], TotalCount, dts[1]).ToString();
        }

        protected override string delrow()
        {
            int DebtId = GetRequestValue<int>("DebtId", 0);        

            bool r = PartsDebtController.Delete(DebtId);
            if (r)
            {               
                SuccessJson("成功删除");
                return "";
            }
            else
            {
                AlertJson("删除失败");
                return "";
            }

        }
    }
}